import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/code_value_entity.dart';
import 'package:ls_work/net/response/event_manage/event_manage_list_data.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/event_manage/add_event_follow_page.dart';
import 'package:ls_work/ui/event_manage/add_event_handle_page.dart';
import 'package:ls_work/ui/event_manage/event_detail_page.dart';
import 'package:ls_work/viewmodel/event_manage/event_manage_list_viewmodel.dart';

import '../../a_yyt/models/floor_list_entity.dart';
import '../../widgets/select_tag_widget.dart';
import '../../widgets/title_value_widget.dart';
import 'add_event_page.dart';

class EventManageListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _EventManageListState();
}

class _EventManageListState
    extends BaseMvvmListState<EventManageListViewModel, EventManageListPage> {
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text("事件处理"),
        actions: [
          IconButton(
              onPressed: () {
                key.currentState?.openEndDrawer();
              },
              icon: Icon(Icons.filter_alt_sharp))
        ],
      ),
      body: Column(
        children: [
          loadingContentWidget.setWeight(1),
          TextButton(
            onPressed: () async {
              var data = await Go().push(AddEventPage());
              if(data!=null){
                viewModel.requestRefresh();
              }
            },
            child: CommonText('提报登记', textColor: Colors.white),
            style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colors.blue),
                shape: WidgetStateProperty.all(StadiumBorder()),
                minimumSize: WidgetStateProperty.all(
                    Size(MediaQuery.of(context).size.width - 32, 49))),
          ),
          SizedBox(
            height: 20,
          )
        ],
      ),
      endDrawer: vm == null
          ? Container()
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                AppBar(
                  title: Text('筛选'),
                  leading: Container(),
                  actions: [Container()],
                ),
                SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      CommonText('事件类型', fontWeight: FontWeight.bold, textSize: 18)
                          .addToContainer(
                          margin: EdgeInsets.only(left: 16, top: 16, bottom: 16)),
                      SelectTagWidget<CodeValue>(
                        items: viewModel.eventTypeList,
                        builder: (item, hasSelected) {
                          return CommonText(item.name,
                              textColor:
                              hasSelected ? Colors.white : Colors.black)
                              .addToContainer(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 16, vertical: 8),
                              decoration: BoxDecoration(
                                  color: hasSelected
                                      ? Colours.app_main
                                      : Colors.white,
                                  borderRadius: 8.borderRadius,
                                  border: Border.all(
                                      color: hasSelected
                                          ? Colours.app_main
                                          : Colors.grey,
                                      width: 1)));
                        },
                        selectedItems: viewModel.selectedType,
                        onSelectListener: (items) {
                          viewModel.selectedType = items;
                          viewModel.requestRefresh();
                        },
                      ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16)),
                      CommonText('楼宇', fontWeight: FontWeight.bold, textSize: 18)
                          .addToContainer(
                          margin: EdgeInsets.only(left: 16, top: 16, bottom: 16)),
                      SelectTagWidget<FloorEntity>(
                        items: viewModel.buildingList,
                        builder: (item, hasSelected) {
                          return CommonText(item.name,
                              textColor:
                              hasSelected ? Colors.white : Colors.black)
                              .addToContainer(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 13, vertical: 8),
                              decoration: BoxDecoration(
                                  color: hasSelected
                                      ? Colours.app_main
                                      : Colors.white,
                                  borderRadius: 8.borderRadius,
                                  border: Border.all(
                                      color: hasSelected
                                          ? Colours.app_main
                                          : Colors.grey,
                                      width: 1)));
                        },
                        selectedItems: viewModel.selectedBuilding,
                        onSelectListener: (items) {
                          // showToast(items.map((e) => e.name).join(","));
                          viewModel.selectedBuilding = items;
                          viewModel.requestRefresh();
                        },
                      ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16)),

                    ],
                  ),
                ).setWeight(1),
                Divider(),
                Row(
                  children: [
                    TextButton(
                            onPressed: () {
                              Go().pop();
                              setState(() {
                                viewModel.selectedBuilding = [];
                                viewModel.selectedType = [];
                              });
                              viewModel.requestRefresh();
                            },
                        style: ButtonStyle(
                            backgroundColor: WidgetStateProperty.all(Colors.white),
                            minimumSize: WidgetStateProperty.all(Size.fromHeight(49)),
                            shape: WidgetStateProperty.all(RoundedRectangleBorder())
                        ),
                            child: CommonText('重置', textColor: Colors.grey))
                        .setWeight(1),
                    TextButton(
                            onPressed: () {
                              Go().pop();
                            },
                        style: ButtonStyle(
                          backgroundColor: WidgetStateProperty.all(Colors.blue),
                          minimumSize: WidgetStateProperty.all(Size.fromHeight(49)),
                          shape: WidgetStateProperty.all(RoundedRectangleBorder())
                        ),
                            child: CommonText('确定', textColor: Colors.white))
                        .setWeight(1),
                  ],
                )
              ],
            ).addToContainer(
              width: MediaQuery.of(context).size.width * 0.7,
              height: double.infinity,
              color: Colors.white),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return itemContent(item);
  }

  @override
  EventManageListViewModel createViewModel() {
    return EventManageListViewModel(context);
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.symmetric(horizontal: 16, vertical: 16);

  @override
  Widget get separatorDivider => SizedBox(
        height: 16,
      );

  Widget itemContent(EventManageListData item) {
    return Column(
      children: [
        Row(
          children: [
            SizedBox(
              width: 16,
            ),
            CommonText('提报登记', textColor: Colors.white),
            SizedBox().setWeight(1),
            CommonText('${item.eventTypeName}', textColor: Colors.white),
            SizedBox(
              width: 16,
            )
          ],
        ).addToContainer(height: 50, color: item.titleColor),
        SizedBox(
          height: 12,
        ),

        if(!item.elderName.isNullOrEmpty())
          ...[
            TitleValueWidget(
              "老人姓名：",
              nameColor: Colors.grey[700]!,
              padding: EdgeInsets.symmetric(horizontal: 16),
              showArrow: false,
              text: "${item.elderName}",
              titleWeight: 2,
              contentWeight: 5,
            ),
            SizedBox(
              height: 12,
            ),
            TitleValueWidget(
              "房间号：",
              nameColor: Colors.grey[700]!,
              padding: EdgeInsets.symmetric(horizontal: 16),
              showArrow: false,
              text: "${item.buildingNo}${item.bedNo}",
              titleWeight: 2,
              contentWeight: 5,
            ),
            SizedBox(
              height: 12,
            ),
            TitleValueWidget(
              "老人编号：",
              nameColor: Colors.grey[700]!,
              padding: EdgeInsets.symmetric(horizontal: 16),
              showArrow: false,
              text: "${item.oldManNumber}",
              titleWeight: 2,
              contentWeight: 5,
            ),
            SizedBox(
              height: 12,
            ),
          ],

        TitleValueWidget(
          "发生时间：",
          nameColor: Colors.grey[700]!,
          padding: EdgeInsets.symmetric(horizontal: 16),
          showArrow: false,
          text: "${item.occurrenceTime}",
          titleWeight: 2,
          contentWeight: 5,
        ),
        SizedBox(
          height: 12,
        ),
        TitleValueWidget(
          "发生位置：",
          nameColor: Colors.grey[700]!,
          padding: EdgeInsets.symmetric(horizontal: 16),
          showArrow: false,
          text: "${item.occurrencePlaceName}",
          titleWeight: 2,
          contentWeight: 5,
        ),
        SizedBox(
          height: 12,
        ),
        TitleValueWidget(
          "详细位置：",
          nameColor: Colors.grey[700]!,
          padding: EdgeInsets.symmetric(horizontal: 16),
          showArrow: false,
          text: "${item.placeDetail}",
          titleWeight: 2,
          contentWeight: 5,
        ),
        SizedBox(
          height: 12,
        ),
        TitleValueWidget(
          "事件经过：",
          nameColor: Colors.grey[700]!,
          padding: EdgeInsets.symmetric(horizontal: 16),
          showArrow: false,
          text: "${item.eventProcess}",
          titleWeight: 2,
          contentWeight: 5,
        ),
        SizedBox(
          height: 12,
        ),
        TitleValueWidget(
          "登记人：",
          nameColor: Colors.grey[700]!,
          padding: EdgeInsets.symmetric(horizontal: 16),
          showArrow: false,
          text: "${item.createdByName}",
          titleWeight: 2,
          contentWeight: 5,
        ),
        SizedBox(
          height: 12,
        ),
        TitleValueWidget(
          "登记时间：",
          nameColor: Colors.grey[700]!,
          padding: EdgeInsets.symmetric(horizontal: 16),
          showArrow: false,
          text: "${item.createdDate}",
          titleWeight: 2,
          contentWeight: 5,
        ),
        SizedBox(
          height: 12,
        ),
        if ((item.handleList??[]).isNotEmpty) ...[
          Row(
            children: [
              SizedBox(
                width: 16,
              ),
              CommonText('事件处理', textColor: Colors.white),
              SizedBox().setWeight(1),
            ],
          ).addToContainer(height: 50, color: item.titleColor),
          ...(item.handleList??[]).map((handle) {
            return Column(
              children: [
                SizedBox(
                  height: 12,
                ),
                TitleValueWidget(
                  "处理记录：",
                  nameColor: Colors.grey[700]!,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  showArrow: false,
                  text: "${handle.detail}",
                  titleWeight: 2,
          contentWeight: 5,
                ),
                SizedBox(
                  height: 12,
                ),
                TitleValueWidget(
                  "登记人：",
                  nameColor: Colors.grey[700]!,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  showArrow: false,
                  text: "${handle.createdBy}",
                  titleWeight: 2,
          contentWeight: 5,
                ),
                SizedBox(
                  height: 12,
                ),
                TitleValueWidget(
                  "登记事件：",
                  nameColor: Colors.grey[700]!,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  showArrow: false,
                  text: "${handle.createdDate}",
                  titleWeight: 2,
          contentWeight: 5,
                ),
                SizedBox(
                  height: 12,
                ),
              ],
            ).addToContainer(
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(
                            color: handle == item.handleList?.last
                                ? Colors.white
                                : Colors.grey[350]!,
                            width: handle == item.handleList?.last ? 0 : 1))));
          }).toList(),
        ],
        if ((item.followList??[]).isNotEmpty) ...[
          Row(
            children: [
              SizedBox(
                width: 16,
              ),
              CommonText('事件跟踪', textColor: Colors.white),
              SizedBox().setWeight(1),
            ],
          ).addToContainer(height: 50, color: item.titleColor),
          ...[item.followList?.first].map((follow) {
            return Column(
              children: [
                SizedBox(
                  height: 12,
                ),
                TitleValueWidget(
                  "跟踪记录：",
                  nameColor: Colors.grey[700]!,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  showArrow: false,
                  text: "${follow?.detail??""}",
                  titleWeight: 2,
          contentWeight: 5,
                ),
                SizedBox(
                  height: 12,
                ),
                TitleValueWidget(
                  "登记人：",
                  nameColor: Colors.grey[700]!,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  showArrow: false,
                  text: "${follow?.createdBy??""}",
                  titleWeight: 2,
          contentWeight: 5,
                ),
                SizedBox(
                  height: 12,
                ),
                TitleValueWidget(
                  "登记时间：",
                  nameColor: Colors.grey[700]!,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  showArrow: false,
                  text: "${follow?.createdDate??""}",
                  titleWeight: 2,
          contentWeight: 5,
                ),
                SizedBox(
                  height: 12,
                ),
              ],
            ).addToContainer(
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(
                            color: follow == item.followList?.last
                                ? Colors.transparent
                                : Colors.grey[350]!,
                            width: follow == item.followList?.last ? 0 : 1))));
          }).toList(),
          SizedBox(
            height: 12,
          ),
        ],
        Divider(
          height: 1,
        ),
        Row(
          children: [
            TextButton(
                    onPressed: () {
                      Go().push(EventDetailPage(
                        item: item,
                      ));
                    },
                    child: CommonText('查看'))
                .setWeight(1),
            if ((item.handleList??[]).isEmpty) ...[
              VerticalDivider(
                width: 1,
              ),
              TextButton(
                      onPressed: () async {
                        var data = await Go().push(AddEventHandlePage(eventId: item.id));
                        if(data!=null){
                          viewModel.requestRefresh();
                        }
                      },
                      child: CommonText('事件处理'))
                  .setWeight(1)
            ],
            if (item.canFollow) ...[
              VerticalDivider(
                width: 1,
              ),
              TextButton(
                      onPressed: () async {
                        var data = await Go().push(AddEventFollowPage(eventId: item.id));
                        if(data!=null){
                          viewModel.requestRefresh();
                        }
                      },
                      child: CommonText('事件跟踪'))
                  .setWeight(1)
            ]
          ],
        ).addToContainer(height: 45)
      ],
    )
        .addToContainer(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: 10.borderRadius))
        .toRound(borderRadius: 10.borderRadius);
  }
}
